﻿//===============================================================================
//	GameHostManager.cpp
//	服务器组主机设置
//===============================================================================

#include "GameHostManager.h"

#include "user_define.h"
#include "log.h"

//-------------------------------------------------------------------------------
const char host_filename[] = "config/HostsConfig.json";

//-------------------------------------------------------------------------------
int32_t GameHostManager::initialize(uint32_t hostId)
{
	_safe_guard_begin;

	printf("Load config/HostsConfig.json start ...\n");

	m_game_host.clear();
	m_game_host = ReadJsonFile(host_filename);
	if (m_game_host == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> config/HostsConfig.json failed ......");
		return -1;
	}

	if (!m_game_host.isMember("area_id"))
	{
		LOG_ERROR("default","ReadJsonFile -> config/HostsConfig.json has no member [\"area_id\"] ......");
		return -1;
	}


	if (!m_game_host.isMember("DatabaseHost"))
	{
		LOG_ERROR("default","ReadJsonFile -> config/HostsConfig.json has no member [\"DatabaseHost\"] ......");
		return -1;
	}

	if (!m_game_host.isMember("CentralHost"))
	{
		LOG_ERROR("default","ReadJsonFile -> config/HostsConfig.json has no member [\"CentralHost\"] ......");
		return -1;
	}

	if (!m_game_host.isMember("ChatHost"))
	{
		LOG_ERROR("default","ReadJsonFile -> config/HostsConfig.json has no member [\"ChatHost\"] ......");
		return -1;
	}
	if (!m_game_host.isMember("gamedb"))
	{
		LOG_ERROR("default","ReadJsonFile -> config/HostsConfig.json has no member [\"gamedb\"] ......");
		return -1;
	}

	m_host_id = hostId;
	m_zone_id			= m_game_host["zone_id"].asInt();
	m_area_id			= m_game_host["area_id"].asInt();
	m_db_host_id		= m_game_host["DatabaseHost"]["id"].asInt();
	m_central_host_id	= m_game_host["CentralHost"]["id"].asInt();
	m_chat_host_id		= m_game_host["ChatHost"]["id"].asInt();

	if (m_game_host.isMember("loginqueue"))
	{
		m_loginqueue = m_game_host["loginqueue"].asInt();
	}

	printf("Load config/HostsConfig.json end.\n");

	return 0;

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
GameHostManager::GameHostManager(void)
	: m_zone_id(1)
	, m_area_id(1)
	, m_host_id(0)
	, m_db_host_id(0)
	, m_central_host_id(0)
	, m_chat_host_id(0)
	, m_loginqueue(0)

{
	_safe_guard_begin;

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
GameHostManager::~GameHostManager(void)
{
}

// 获取服务器的区服Id
uint32_t GameHostManager::GetServerZoneId(void)
{
	return m_zone_id;
}

// 获取服务器的区服Id
uint32_t GameHostManager::GetServerAreaId(void)
{
	return m_area_id;
}

//-------------------------------------------------------------------------------
uint32_t GameHostManager::GetDatabaseServerId(void)
{
	_safe_guard_begin;

	return m_db_host_id;

	_safe_guard_end;

	return 0;
}

//-------------------------------------------------------------------------------
uint32_t GameHostManager::GetCenterServerId(void)
{
	_safe_guard_begin;

	return m_central_host_id;

	_safe_guard_end;

	return 0;
}

//-------------------------------------------------------------------------------
uint32_t GameHostManager::GetChatServerId(void)
{
	_safe_guard_begin;

	return m_chat_host_id;

	_safe_guard_end;

	return 0;
}

uint32_t GameHostManager::GetMapServerId(void)
{
	_safe_guard_begin;

	return m_map_host_id;

	_safe_guard_end;

	return 0;
}

uint32_t GameHostManager::GetLoginqueue(void)
{
	_safe_guard_begin;

	return m_loginqueue;

	_safe_guard_end;
}

uint32_t GameHostManager::GetHostId(void)
{
	_safe_guard_begin;

	return m_host_id;

	_safe_guard_end;

	return 0;
}

//-------------------------------------------------------------------------------
std::string GameHostManager::GetHostKey(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("host_key"))
	{
		LOG_ERROR("default","ReadJsonFile -> config/HostsConfig.json has no member [\"host_key\"] ......");
		return "";
	}
	
	return m_game_host["host_key"].asString();

	_safe_guard_end;

	return "";
}

//-------------------------------------------------------------------------------
uint16_t GameHostManager::GetGameHostListenPort(uint32_t hostId)
{
	_safe_guard_begin;

	if (hostId == 0)
	{
		hostId = m_host_id;
	}

	if (0 == hostId)
	{
		return 0;
	}

	char _idx_name[32];
	memset(_idx_name, 0, 32);

	snprintf(_idx_name, 32, "GameHost_%d", hostId);

	if (!m_game_host.isMember(_idx_name))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetGameHostListenPort has no member %s", _idx_name);
		return 0;
	}

	uint16_t _port = m_game_host[_idx_name]["port"].asInt();

	return _port;

	_safe_guard_end;

	return 0;
}

// 是否启用自适应选服
uint16_t GameHostManager::GetGameHostApposite()
{
	_safe_guard_begin;

	if (!m_game_host.isMember("appositeGame"))
	{
		return 0;
	}

	uint16_t apposite = m_game_host["appositeGame"].asInt();
	return apposite;

	_safe_guard_end;

	return 0;
}

std::string GameHostManager::GetGameHostIp(uint32_t hostId)
{
	_safe_guard_begin;

	if (hostId == 0)
	{
		hostId = m_host_id;
	}

	if (0 == hostId)
	{
		return "";
	}

	char _idx_name[32];
	memset(_idx_name, 0, 32);

	snprintf(_idx_name, 32, "GameHost_%d", hostId);

	if (!m_game_host.isMember(_idx_name))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetGameHostListenPort has no member %s", _idx_name);
		return "";
	}

	std::string _ip = m_game_host[_idx_name]["host"].asString();

	return _ip;

	_safe_guard_end;

	return "";
}

//-------------------------------------------------------------------------------
uint16_t GameHostManager::GetCenterHostListenPort(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("CentralHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetCenterHostListenPort has no member \"CentralHost\"");
		return 0;
	}

	uint16_t _port = m_game_host["CentralHost"]["port"].asInt();

	return _port;

	_safe_guard_end;

	return 0;
}

//-------------------------------------------------------------------------------
std::string GameHostManager::GetCenterHostIp(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("CentralHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetCenterHostIp has no member \"CentralHost\"");
		return 0;
	}

	std::string _ip = m_game_host["CentralHost"]["host"].asString();

	return _ip;

	_safe_guard_end;

	return "";
}

//-------------------------------------------------------------------------------
uint16_t GameHostManager::GetDatabaseHostListenPort(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("DatabaseHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetDatabaseHostListenPort has no member \"DatabaseHost\"");
		return 0;
	}

	uint16_t _port = m_game_host["DatabaseHost"]["port"].asInt();

	return _port;

	_safe_guard_end;

	return 0;
}

//-------------------------------------------------------------------------------
std::string GameHostManager::GetDatabaseHostIp(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("DatabaseHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetDatabaseHostIp has no member \"DatabaseHost\"");
		return 0;
	}

	std::string _ip = m_game_host["DatabaseHost"]["host"].asString();

	return _ip;

	_safe_guard_end;

	return "";
}

//-------------------------------------------------------------------------------
uint16_t GameHostManager::GetChatHostListenPort(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("ChatHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetChatHostListenPort has no member \"ChatHost\"");
		return 0;
	}

	uint16_t _port = m_game_host["ChatHost"]["port"].asInt();

	return _port;

	_safe_guard_end;

	return 0;
}

//-------------------------------------------------------------------------------
std::string GameHostManager::GetChatHostIp(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("ChatHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetChatHostIp has no member \"ChatHost\"");
		return 0;
	}

	std::string _ip = m_game_host["ChatHost"]["host"].asString();

	return _ip;

	_safe_guard_end;

	return "";
}

uint16_t GameHostManager::GetMapHostListenPort(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("MapHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetMapHostListenPort has no member \"MapHost\"");
		return 0;
	}

	uint16_t _port = m_game_host["MapHost"]["port"].asInt();

	return _port;

	_safe_guard_end;

	return 0;
}

std::string GameHostManager::GetMapHostIp(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("MapHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetMapHostIp has no member \"MapHost\"");
		return 0;
	}

	std::string _ip = m_game_host["MapHost"]["host"].asString();

	return _ip;

	_safe_guard_end;

	return "";
}


int32_t GameHostManager::GetMapTraceLevel(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("MapHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetMapTraceLevel has no member \"MapHost\"");
		return -1;
	}

	int32_t traceLevel = m_game_host["MapHost"]["traceLevel"].asInt();

	return traceLevel;

	_safe_guard_end;

	return -1;
}

int32_t GameHostManager::GetMapId(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("MapHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetMapId has no member \"MapHost\"");
		return -1;
	}

	int32_t mid = m_game_host["MapHost"]["mapid"].asInt();

	return mid;

	_safe_guard_end;

	return 0;
}


//-------------------------------------------------------------------------------
uint16_t GameHostManager::GetRankHostListenPort(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("RankHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetRankHostListenPort has no member \"RankHost\"");
		return 0;
	}

	uint16_t _port = m_game_host["ChatHost"]["port"].asInt();

	return _port;

	_safe_guard_end;

	return 0;
}

//-------------------------------------------------------------------------------
std::string GameHostManager::GetRankHostIp(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("RankHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetRankHostIp has no member \"RankHost\"");
		return 0;
	}

	std::string _ip = m_game_host["RankHost"]["host"].asString();

	return _ip;

	_safe_guard_end;

	return "";
}

uint16_t GameHostManager::GetLoginHostListenPort(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("LoginHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetLoginHostListenPort has no member \"LoginHost\"");
		return 0;
	}

	uint16_t _port = m_game_host["LoginHost"]["port"].asInt();

	return _port;

	_safe_guard_end;

	return 0;
}

std::string GameHostManager::GetLoginHostIp(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("LoginHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetLoginHostIp has no member \"LoginHost\"");
		return 0;
	}

	std::string _ip = m_game_host["LoginHost"]["host"].asString();

	return _ip;

	_safe_guard_end;

	return "";
}
// -------------------------------------gamedb--------------------------
std::string GameHostManager::GetGameDBIP()
{
	_safe_guard_begin;

	if (!m_game_host.isMember("gamedb"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetRankHostIp has no member \"RankHost\"");
		return "";
	}

	std::string _ip = m_game_host["gamedb"]["host"].asString();

	return _ip;

	_safe_guard_end;

	return "";
}

std::string GameHostManager::GetGameDBProt()
{
	_safe_guard_begin;

	if (!m_game_host.isMember("gamedb"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetRankHostIp has no member \"RankHost\"");
		return "";
	}

	std::string _prot = m_game_host["gamedb"]["port"].asString();

	return _prot;

	_safe_guard_end;

	return "";
}

std::string GameHostManager::GetGameDBUser()
{
	_safe_guard_begin;

	if (!m_game_host.isMember("gamedb"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetRankHostIp has no member \"RankHost\"");
		return "";
	}

	std::string _user = m_game_host["gamedb"]["usr"].asString();

	return _user;

	_safe_guard_end;

	return "";
}

std::string GameHostManager::GetGameDBPwd()
{
	_safe_guard_begin;

	if (!m_game_host.isMember("gamedb"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetRankHostIp has no member \"RankHost\"");
		return "";
	}

	std::string _passwd = m_game_host["gamedb"]["passwd"].asString();

	return _passwd;

	_safe_guard_end;

	return "";
}

std::string GameHostManager::GetGameDBName()
{
	_safe_guard_begin;

	if (!m_game_host.isMember("gamedb"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetRankHostIp has no member \"RankHost\"");
		return "";
	}

	std::string _name = m_game_host["gamedb"]["name"].asString();

	return _name;

	_safe_guard_end;

	return "";
}

// -------------------------------------accountdb--------------------------
std::string GameHostManager::GetAccountDBIP()
{
	_safe_guard_begin;

	if (!m_game_host.isMember("accdb"))
	{
		LOG_ERROR("default", "Error >>> GameHostManager::GetRankHostIp has no member \"RankHost\"");
		return "";
	}

	std::string _ip = m_game_host["accdb"]["host"].asString();

	return _ip;

	_safe_guard_end;

	return "";
}

std::string GameHostManager::GetAccountDBProt()
{
	_safe_guard_begin;

	if (!m_game_host.isMember("accdb"))
	{
		LOG_ERROR("default", "Error >>> GameHostManager::GetRankHostIp has no member \"RankHost\"");
		return "";
	}

	std::string _prot = m_game_host["accdb"]["port"].asString();

	return _prot;

	_safe_guard_end;

	return "";
}

std::string GameHostManager::GetAccountDBUser()
{
	_safe_guard_begin;

	if (!m_game_host.isMember("accdb"))
	{
		LOG_ERROR("default", "Error >>> GameHostManager::GetRankHostIp has no member \"RankHost\"");
		return "";
	}

	std::string _user = m_game_host["accdb"]["usr"].asString();

	return _user;

	_safe_guard_end;

	return "";
}

std::string GameHostManager::GetAccountDBPwd()
{
	_safe_guard_begin;

	if (!m_game_host.isMember("accdb"))
	{
		LOG_ERROR("default", "Error >>> GameHostManager::GetRankHostIp has no member \"RankHost\"");
		return "";
	}

	std::string _passwd = m_game_host["accdb"]["passwd"].asString();

	return _passwd;

	_safe_guard_end;

	return "";
}

std::string GameHostManager::GetAccountDBName()
{
	_safe_guard_begin;

	if (!m_game_host.isMember("accdb"))
	{
		LOG_ERROR("default", "Error >>> GameHostManager::GetRankHostIp has no member \"RankHost\"");
		return "";
	}

	std::string _name = m_game_host["accdb"]["name"].asString();

	return _name;

	_safe_guard_end;

	return "";
}
// ---------------------------------------------------------------
uint16_t GameHostManager::GetProxyHostListenPort(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("ProxyHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetProxyHostListenPort has no member \"ProxyHost\"");
		return 0;
	}

	uint16_t _port = m_game_host["ProxyHost"]["port"].asInt();

	return _port;

	_safe_guard_end;

	return 0;
}

std::string GameHostManager::GetProxyHostIp(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("ProxyHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetProxyHostIp has no member \"ProxyHost\"");
		return 0;
	}

	std::string _ip = m_game_host["ProxyHost"]["host"].asString();

	return _ip;

	_safe_guard_end;

	return "";
}

uint16_t GameHostManager::GetWorldProxyHostListenPort(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("WorldProxyHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetWorldProxyHostListenPort has no member \"WorldProxyHost\"");
		return 0;
	}

	uint16_t _port = m_game_host["WorldProxyHost"]["port"].asInt();

	return _port;

	_safe_guard_end;

	return 0;
}

std::string GameHostManager::GetWorldProxyHostIp(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("WorldProxyHost"))
	{
		LOG_ERROR("default","Error >>> GameHostManager::GetWorldProxyHostIp has no member \"WorldProxyHost\"");
		return 0;
	}

	std::string _ip = m_game_host["WorldProxyHost"]["host"].asString();

	return _ip;

	_safe_guard_end;

	return "";
}

uint16_t GameHostManager::GetPlatformProxyHostListenPort(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("PlatformProxyHost"))
	{
		LOG_ERROR("default", "Error >>> GameHostManager::GetPlatformProxyHostListenPort has no member \"PlatformProxyHost\"");
		return 0;
	}

	uint16_t _port = m_game_host["PlatformProxyHost"]["port"].asInt();

	return _port;

	_safe_guard_end;

	return 0;
}

std::string GameHostManager::GetPlatformProxyHostIp(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("PlatformProxyHost"))
	{
		LOG_ERROR("default", "Error >>> GameHostManager::GetPlatformProxyHostIp has no member \"PlatformProxyHost\"");
		return 0;
	}

	std::string _ip = m_game_host["PlatformProxyHost"]["host"].asString();

	return _ip;

	_safe_guard_end;

	return "";
}

uint16_t GameHostManager::GetGateHostListenPort(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("GateHost"))
	{
		LOG_ERROR("default", "Error >>> GameHostManager::GetGateHostListenPort has no member \"GateHost\"");
		return 0;
	}

	uint16_t _port = m_game_host["GateHost"]["port"].asInt();

	return _port;

	_safe_guard_end;

	return 0;
}

std::string GameHostManager::GetGateHostIp(void)
{
	_safe_guard_begin;

	if (!m_game_host.isMember("GateHost"))
	{
		LOG_ERROR("default", "Error >>> GameHostManager::GetGateHostIp has no member \"GateHost\"");
		return 0;
	}

	std::string _ip = m_game_host["GateHost"]["host"].asString();

	return _ip;

	_safe_guard_end;

	return "";
}

void GameHostManager::SetMapConnectInfo(std::string& ip, int32_t port /* = 5684 */)
{
	// 修改map 连接信息
	m_game_host["MapHost"]["host"] = ip;
	if (port > 0) {
		m_game_host["MapHost"]["port"] = port;
	}
}
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
